﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace Common
{
   public  class OperateConfig
    {
        /// <summary>
        /// GET Position Server Config
        /// </summary>
        public static Model.ConfigInfo GetPosServerConfig()
        {
            Model.ConfigInfo config = new ConfigInfo();
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            config.AutoStart = myini.ReadString("SERVERCONFIG", "AutoStart", "0");
            config.Com = myini.ReadString("SERVERCONFIG", "Com", "COM1");
            config.DatBaseName = myini.ReadString("DBCONFIG", "DataBaseName", "TaxiDispatchSystem");
            config.PassWord = myini.ReadString("DBCONFIG", "PassWord", "123456");
            config.ServerName = myini.ReadString("DBCONFIG", "ServerName", "127.0.0.1");
            config.UserId = myini.ReadString("DBCONFIG", "UserID", "root");
            
            return config;
        }


        public static string GetAdContent()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            return myini.ReadString("SERVERCONFIG", "AD", "");
        }

        public static string GetTheNewestClientVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string config = myini.ReadString("SERVERCONFIG", "StationClientVersion", "1.0");
            if (config == "1.0")
            {
                SaveTheNewestClientVersion(config);
            }
            return config;
        }

        /// <summary>
        /// Save AcceptServer Config[]  0--PositionServerIP ,1--AcceptServerIp
        /// </summary>
        /// <param name="config"></param>
        public static void SaveTheNewestClientVersion(string config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("SERVERCONFIG", "StationClientVersion", config);
        }

        /// <summary>
        /// Save Position Server Config
        /// </summary>
        public static void SavePosServerConfig(string autoStart,string com)
        {

            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("SERVERCONFIG", "Com", com);
            myini.WriteString("SERVERCONFIG", "AutoStart", autoStart);
        }

        /// <summary>
        /// Save Position Server Config
        /// </summary>
        public static void SavePosServerConfig(Model.ConfigInfo config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("SERVERCONFIG", "AutoStart", config.AutoStart);
            myini.WriteString("DBCONFIG", "DataBaseName", config.DatBaseName);
            myini.WriteString("DBCONFIG", "ServerName", config.ServerName);
            myini.WriteString("DBCONFIG", "UserID", config.UserId);
            myini.WriteString("DBCONFIG", "PassWord", config.PassWord);
        }




        /// <summary>
        /// Get AcceptServer Config[]  0--PositionServerIP ,1--AcceptServerIp 
        /// 5-DataBaseName，6-ServerName,7-UserID，8-PassWord
        /// </summary>
        /// <returns></returns>
        public static string[] GetAcceptServerConfig()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string[] config = new string[9];
            config[0] = myini.ReadString("SERVERCONFIG", "PositionServerIP", "127.0.0.1");
            config[1] = myini.ReadString("SERVERCONFIG", "AcceptServerIp", "127.0.0.1");
            config[2] = myini.ReadString("SERVERCONFIG", "AcceptServerWebIp", "127.0.0.1");
            config[3] = myini.ReadString("SERVERCONFIG", "PhoneNumber", "18602789588");
            config[4] = myini.ReadString("SERVERCONFIG", "TransferServerIP", "127.0.0.1");



            config[5] = myini.ReadString("DBCONFIG", "DataBaseName", "TaxiDispatchSystem");
            config[6] = myini.ReadString("DBCONFIG", "ServerName", "127.0.0.1");
            config[7] = myini.ReadString("DBCONFIG", "UserID", "root");
            config[8] = myini.ReadString("DBCONFIG", "PassWord", "123456");

            return config;
        }

        /// <summary>
        /// Save AcceptServer Config[]  0--PositionServerIP ,1--AcceptServerIp
        /// </summary>
        /// <param name="config"></param>
        public static void SaveAcceptServerConfig(string[] config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("SERVERCONFIG", "PositionServerIP", config[0]);
            myini.WriteString("SERVERCONFIG", "AcceptServerIp", config[1]);
            myini.WriteString("SERVERCONFIG", "AcceptServerWebIp", config[2]);
            myini.WriteString("SERVERCONFIG", "PhoneNumber", config[3]);
            myini.WriteString("SERVERCONFIG", "TransferServerIP", config[4]);


            //myini.WriteString("DBCONFIG", "DataBaseName", config[5]);
            //myini.WriteString("DBCONFIG", "ServerName", config[6]);
            //myini.WriteString("DBCONFIG", "UserID", config[7]);
            //myini.WriteString("DBCONFIG", "PassWord", config[8]);

        }






        /// <summary>
        /// 0-DataBaseName,1-ServerName,2-UserID，3-PassWord
        /// </summary>
        /// <param name="config"></param>
        public static void SaveAcceptServerDataBaseConfig(string[] config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("DBCONFIG", "DataBaseName", config[0]);
            myini.WriteString("DBCONFIG", "ServerName", config[1]);
            myini.WriteString("DBCONFIG", "UserID", config[2]);
            myini.WriteString("DBCONFIG", "PassWord", config[3]);

        }


        ///// <summary>
        ///// Client Save ServerIp
        ///// </summary>
        //public static void SaveClientConfig(string ip)
        //{
        //    IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
        //    myini.WriteString("CONFIG", "ServerIp", ip);
        //}

        ///// <summary>
        ///// Client Save ServerIp
        ///// </summary>
        //public static string  GetClientConfig()
        //{
        //    IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
        //   return  myini.ReadString("CONFIG", "ServerIp", "192.168.0.25");
        //}

        /// <summary>
        /// Client Save ServerIp
        /// </summary>
        public static string GetCenterPhoneNumber()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            return myini.ReadString("SERVERCONFIG", "PhoneNumber", "18602789588");
        }


        /// <summary>
        /// Client Save ServerIp
        /// </summary>
        public static string GetPhoneUserClientVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            return myini.ReadString("SERVERCONFIG", "PhoneUserClientVersion", "1.0");
        }


        /// <summary>
        /// Client Save ServerIp
        /// </summary>
        public static string GetTaxiClientVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            return myini.ReadString("SERVERCONFIG", "TaxiClientVersion", "1.0");
        }

        /// <summary>
        /// Client Save ServerIp
        /// </summary>
        public static string GetPrivateCarClientVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string version =myini.ReadString("SERVERCONFIG", "PrivateCarClientVersion", "1.0");
            if (version=="1.0")
            {
                myini.WriteString("SERVERCONFIG", "PrivateCarClientVersion", "1.0");    
            }            
            return version;
        }
  
        public static string GetSpecialCarClientVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string version = myini.ReadString("SERVERCONFIG", "SpecialCarClientVersion", "100");
            if (version == "100")
            {
                myini.WriteString("SERVERCONFIG", "SpecialCarClientVersion", "100");
            }
            return version;
        }

        public static string GetPhonePlatformVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string version = myini.ReadString("SERVERCONFIG", "PhonePlatformVersion", "100");
            if (version == "100")
            {
                myini.WriteString("SERVERCONFIG", "PhonePlatformVersion", "100");
            }
            return version;
        }


        public static string GetPlatformUrlConfig()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string url = myini.ReadString("SERVERCONFIG", "WebUrl",
                "http://hbwdf.cn:8888/DaTongWeb/login.jsp");
            if (url == "http://hbwdf.cn:8888/DaTongWeb/login.jsp")
            {
                myini.WriteString("SERVERCONFIG", "WebUrl",
                    "http://hbwdf.cn:8888/DaTongWeb/login.jsp");
            }
            return url;
        }

        /// <summary>
        /// Client Save TransferServerIp
        /// </summary>
        public static void SaveClientTransferServerIp(string ip)
        {
            IniFiles myini = new IniFiles(Application.StartupPath + "\\system.ini");
            myini.WriteString("CONFIG", "TransferServerIp", ip);
        }

        /// <summary>
        /// Client get TransferServerIp
        /// </summary>
        public static string GetClientTransferServerIp()
        {
            IniFiles myini = new IniFiles(Application.StartupPath + "\\system.ini");
            return myini.ReadString("CONFIG", "TransferServerIp", "192.168.0.253");
        }


        /// <summary>
        /// Client Save MainFormTitle
        /// </summary>
        public static void SaveClientMainFormTitle(string title)
        {
            IniFiles myini = new IniFiles(Application.StartupPath + "\\system.ini");
            myini.WriteString("CONFIG", "MainFormTitle", title);
        }

        /// <summary>
        /// Client get MainFormTitle
        /// </summary>
        public static string GetClientMainFormTitle()
        {
            IniFiles myini = new IniFiles(Application.StartupPath + "\\system.ini");
            string title =myini.ReadString("CONFIG", "MainFormTitle",
                "出租车智能电召系统").Trim('\0');

            if (title=="出租车智能电召系统")
            {
                SaveClientMainFormTitle(title);
            }
            return title;
        }



        /// <summary>
        /// Client get MainFormTitle
        /// </summary>
        public static string GetClientDrawRouteType()
        {
            IniFiles myini = new IniFiles(Application.StartupPath + "\\system.ini");
            string title = myini.ReadString("CONFIG", "DrawRouteType",
                "1").Trim('\0');

            if (title == "1")
            {
                SaveClientDrawRouteType("1");
            }
            return title;
        }

        /// <summary>
        /// Client get MainFormTitle
        /// </summary>
        public static void SaveClientDrawRouteType(string type)
        {
            IniFiles myini = new IniFiles(Application.StartupPath + "\\system.ini");
            myini.WriteString("CONFIG", "DrawRouteType", type); 
        }




        /// <summary>
        /// Client Save OffSetLatitude
        /// </summary>
        public static string GetOffSetLongitude()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            return myini.ReadString("SERVERCONFIG", "OffSetLongitude", "00005723");
        }


        /// <summary>
        /// Client get OffSetLatitude
        /// </summary>
        public static string GetOffSetLatitude()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            return myini.ReadString("SERVERCONFIG", "OffSetLatitude", "00002287");
        }


        public  static string[] GetClientTaxiWarmingConfig()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string[] config = new string[6];            
            config[0] = myini.ReadString("CONFIG", "TaxiSpeedOut", "70");
            config[1] = myini.ReadString("CONFIG", "TaxiSpeedOutIsOpen", "0");
            config[2] = myini.ReadString("CONFIG", "TaxiStopTimeOut", "5");
            config[3] = myini.ReadString("CONFIG", "TaxiStopTimeOutIsOpen", "5");
            config[4] = myini.ReadString("CONFIG", "PowerLeftTime", "4");            
            config[5] = myini.ReadString("CONFIG", "PowerLeftIsOpne", "4");                        
            return config;
        }

        public static void SaveClientTaxiWarmingConfig(string[] config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("CONFIG", "TaxiSpeedOut", config[0]);
            myini.WriteString("CONFIG", "TaxiSpeedOutIsOpen", config[1]);
            myini.WriteString("CONFIG", "TaxiStopTimeOut", config[2]);
            myini.WriteString("CONFIG", "TaxiStopTimeOutIsOpen", config[3]);
            myini.WriteString("CONFIG", "PowerLeftTime", config[4]);
            myini.WriteString("CONFIG", "PowerLeftTimeIsOpne", config[5]);     
        }

        public static string GetClientVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string config = myini.ReadString("SERVERCONFIG", "ClientVersion", "1.00");            
            return config;
        }

        #region  Station 
       
       /// <summary>
       /// 获取站点配置信息：0-serverip,1-端口，2-查询间隔时间，3-本站号码，4-广告内容，5-窗体是否最前
       /// 6-纬度，7-经度，8-zoom值,9-退出程序密码
       /// </summary>
       /// <returns></returns>
        public static string[] GetStationConfig()
        {
            string[] config = new string[11];
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            config[0] = myini.ReadString("SERVERCONFIG", "SERVERIP", "gqcd.cityofcar.com");
            config[1] = myini.ReadString("SERVERCONFIG", "SERVERPORT", ConstParameters.AstPort.ToString());
            config[2] = myini.ReadString("SERVERCONFIG", "UPDATESLEEPTIME", "10000");
            config[3] = myini.ReadString("SERVERCONFIG", "PHONENUMBER", "00000000000");
            config[4] = myini.ReadString("SERVERCONFIG", "ADCONTENT", "智慧城市-出租车智能调度系统").Trim('\0');
            config[5] = myini.ReadString("SERVERCONFIG", "IsTopMost", "1");
            config[6] = myini.ReadString("SERVERCONFIG", "LAT", "29.24828790");
            config[7] = myini.ReadString("SERVERCONFIG", "LNG", "115.78491210");
            config[8] = myini.ReadString("SERVERCONFIG", "ZOOM", "15");
            config[9] = myini.ReadString("SERVERCONFIG", "XmlVersion", "0");
            config[10] = myini.ReadString("SERVERCONFIG", "IsShowExitButton", "0");

            if (config[0] == "gqcbus.cityofcar.com"
                && config[1] == ConstParameters.AstPort.ToString()
                && config[2] == "10000"
                && config[3] == "00000000000"
                && config[4] == "智慧城市-出租车智能调度系统"
                && config[5] == "1"
                && config[6] == "29.24828790"
                && config[7] == "115.78491210"
                && config[8] == "15"                                 
                )
            {
                SaveStationConfig(config);
            }
           if (config[9]=="0")
           {
               SaveStationXmlConfig("0");
           }
           if (config[10] == "0")
           {
               SaveIsShowExitButon("0");
           }
            return config;
        }
        
        public static void SaveStationConfig(string[] config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("SERVERCONFIG", "SERVERIP", config[0]);
            myini.WriteString("SERVERCONFIG", "SERVERPORT", config[1]);
            myini.WriteString("SERVERCONFIG", "UPDATESLEEPTIME", config[2]);
            myini.WriteString("SERVERCONFIG", "PHONENUMBER", config[3]);
            myini.WriteString("SERVERCONFIG", "ADCONTENT", config[4]);
            myini.WriteString("SERVERCONFIG", "IsTopMost", config[5]);
            myini.WriteString("SERVERCONFIG", "LAT", config[6]);
            myini.WriteString("SERVERCONFIG", "LNG", config[7]);
            myini.WriteString("SERVERCONFIG", "ZOOM", config[8]);            
        }

        public static void SaveStationXmlConfig(string version)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("SERVERCONFIG", "XmlVersion", version);                       
        }

        public static void SaveIsShowExitButon(string isshowBtn)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");        
            myini.WriteString("SERVERCONFIG", "IsShowExitButton", isshowBtn);
        }


        /// <summary>
        /// For AcceptServer
        /// </summary>
        /// <returns></returns>
        public static string GetStationXmlVersion()
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            string version = myini.ReadString("SERVERCONFIG", "XmlVersion", "0");
            if (version=="0")
            {
                SaveStationXmlConfig("0");
            }
            return version;
        }

        public static string[] GetStationPwdConfig()
        {
            string[] config = new string[2];
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");                      
            config[0] = myini.ReadString("SERVERCONFIG", "ExitPassWord", "8");
            config[1] = myini.ReadString("SERVERCONFIG", "SetPositionPassWord", "10");
            if ( config[0] == "8" && config[1] == "10" )
            {
                SaveStationPwd(config);
            }
            return config;
        }
        public static void SaveStationPwd(string[] config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");

            myini.WriteString("SERVERCONFIG", "ExitPassWord", config[0]);
            myini.WriteString("SERVERCONFIG", "SetPositionPassWord", config[1]);
        }
       

        public static void SaveStationPosition(string[] config)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
           
            myini.WriteString("SERVERCONFIG", "LAT", config[0]);
            myini.WriteString("SERVERCONFIG", "LNG", config[1]);
        }

        #endregion



        public static void SaveClientChargingConfig(Model.TaxiChargeInfo model)
        {
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            myini.WriteString("CHARGINGCONFIG", "DayInitPrice", model.DayInitPrice.ToString());
            myini.WriteString("CHARGINGCONFIG", "DayInitDistance", model.DayInitDistance.ToString());
            myini.WriteString("CHARGINGCONFIG", "DayOverSubsidy", model.DayOverSubsidy.ToString());
            myini.WriteString("CHARGINGCONFIG", "DayOverPrice", model.DayOverPrice.ToString());
            myini.WriteString("CHARGINGCONFIG", "DayEmptyBackDistance", model.DayEmptyBackDistance.ToString());
            myini.WriteString("CHARGINGCONFIG", "DayEmptyBackSubsidy",model.DayEmptyBackSubsidy.ToString());
            myini.WriteString("CHARGINGCONFIG", "DayWaiteTime", model.DayWaiteTime.ToString());

            myini.WriteString("CHARGINGCONFIG", "NightInitPrice", model.NightInitPrice.ToString());
            myini.WriteString("CHARGINGCONFIG", "NightInitDistance", model.NightInitDistance.ToString());
            myini.WriteString("CHARGINGCONFIG", "NightOverSubsidy", model.NightOverSubsidy.ToString());
            myini.WriteString("CHARGINGCONFIG", "NightOverPrice", model.NightOverPrice.ToString());
            myini.WriteString("CHARGINGCONFIG", "NightEmptyBackDistance", model.NightEmptyBackDistance.ToString());
            myini.WriteString("CHARGINGCONFIG", "NightEmptyBackSubsidy", model.NightEmptyBackSubsidy.ToString());
            myini.WriteString("CHARGINGCONFIG", "NightWaiteTime", model.NightWaiteTime.ToString());
        }

        /// <summary>
        /// For AcceptServer
        /// </summary>
        /// <returns></returns>
        public static TaxiChargeInfo GetClientChargingModel()
        {           
            IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
            float dayInitPrice;
            float dayInitDistance;
            float dayOverSubsidy;
            float dayOverPrice;
            float dayEmptyBackDistance;
            float dayEmptyBackSubsidy;
            float dayWaiteTime;

            float nightInitPrice;
            float nightInitDistance;
            float nightOverSubsidy;
            float nightOverPrice;
            float nightEmptyBackDistance;
            float nightEmptyBackSubsidy;
            float nightWaiteTime;

            float.TryParse(myini.ReadString("CHARGINGCONFIG", "DayInitPrice", "5"), out dayInitPrice);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "DayInitDistance", "1.5"), out dayInitDistance);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "DayOverSubsidy", "0.7"), out dayOverSubsidy);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "DayOverPrice", "1.4"), out dayOverPrice);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "DayEmptyBackDistance", "8"), out dayEmptyBackDistance);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "DayEmptyBackSubsidy", "30"), out dayEmptyBackSubsidy);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "DayWaiteTime", "300"), out dayWaiteTime);

            float.TryParse(myini.ReadString("CHARGINGCONFIG", "NightInitPrice", "6.5"), out nightInitPrice);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "NightInitDistance", "1.5"), out nightInitDistance);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "NightOverSubsidy", "0.9"), out nightOverSubsidy);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "NightOverPrice", "2.1"), out nightOverPrice);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "NightEmptyBackDistance", "8"), out nightEmptyBackDistance);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "NightEmptyBackSubsidy", "30"), out nightEmptyBackSubsidy);
            float.TryParse(myini.ReadString("CHARGINGCONFIG", "NightWaiteTime", "300"), out nightWaiteTime);

            TaxiChargeInfo model = new TaxiChargeInfo();
            model.DayInitPrice = dayInitPrice;
            model.DayInitDistance = dayInitDistance;
            model.DayOverSubsidy = dayOverSubsidy;
            model.DayOverPrice = dayOverPrice;
            model.DayEmptyBackDistance = dayEmptyBackDistance;
            model.DayEmptyBackSubsidy = dayEmptyBackSubsidy;
            model.DayWaiteTime = dayWaiteTime;

            model.NightInitPrice = nightInitPrice;
            model.NightInitDistance = nightInitDistance;
            model.NightOverSubsidy = nightOverSubsidy;
            model.NightOverPrice = nightOverPrice;
            model.NightEmptyBackDistance = nightEmptyBackDistance;
            model.NightEmptyBackSubsidy = nightEmptyBackSubsidy;
            model.NightWaiteTime = nightWaiteTime;
            string sss = model.DayInitPrice.ToString();
            if (model.DayInitPrice.ToString() == "5"
                && model.DayInitDistance.ToString() == "1.5"
                && model.DayOverSubsidy.ToString() == "0.7"
                && model.DayOverPrice.ToString() == "1.4"
                && model.DayEmptyBackDistance.ToString() == "8"
                && model.DayEmptyBackSubsidy.ToString() == "30"
                )
            {
                SaveClientChargingConfig(model);
            }
            return model;
        }




    }
}
